import pymysql.cursors
import time
# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='root',
                             db='iot',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
def get_course():
    localtime = time.localtime(time.time())
    day_idx = localtime.tm_wday + 1
    # print(day_idx)
    try:
        with connection.cursor() as cursor:
            # Read a single record
            sql = "select * from course where `weekday`=%s ORDER BY time"
            cursor.execute(sql, (str(day_idx),))
            result = cursor.fetchall()
            # print(result)
            course_list = []
            for course in result:
                t = course['time']
                begin,end = t.split('-')
                h = localtime.tm_hour
                m = localtime.tm_min
                t_now = str(h) + ':' + str(m)
                if t_now < end:
                    course_list.append(course)
                    if len(course_list) == 2:
                        break    
            # print(course_list)        
            return course_list
    finally:
        pass
        # connection.close()
get_course()